package Dungeoneering.PathFinding;

import java.util.ArrayList;

import org.powerbot.game.api.methods.Widgets;

import Dungeoneering.Entities.Door;
import Dungeoneering.Entities.Room;
import Dungeoneering.PathFinding.DataStructures.QuaternaryTree;

/**
 * The PathProcessor will keep a list of all doors and rooms in the floor. It will also calculate the path between 2 rooms
 * The RoomDetector is utilised here as well for more info check out RoomDetector
 * 
 * NOTE: this class should ONLY be used in DungeoneeringTasks!
 * @author Nicolaas
 *
 */
public class PathProcessor {
	
	private static final ArrayList<Room> rooms = new ArrayList<Room>();
	private static final ArrayList<Door> doors = new ArrayList<Door>();
	private static Room startRoom;
	private static Room bossRoom;
	private static RoomDetector detector;
	
	private static QuaternaryTree tree;
	
	public static Room getstRoom() {
		return startRoom;
	}
	
	/**
	 * this HAS to be called first, or the tree and detector won't get initialised
	 * @param startPositionX the x coordinate you were standing on when entering the dungeon
	 * @param startPositionY the y coordinate you were standing on when entering the dungeon
	 * @param mapX the x-coordinate of the room on the map
	 * @param mapY the y-coordinate of the room on the map
	 */
	public static void setStartRoom(int startPositionX, int startPositionY, int mapX, int mapY) {
		Room r = new Room(mapX, mapY, startPositionX, startPositionY);
		startRoom = r;
		tree = new QuaternaryTree(r);
		detector = new RoomDetector(r);
		detector.start();
	}
	
	public static Room getBossRoom() {
		return bossRoom;
	}
	
	public static void setBossRoom(Room r) {
		bossRoom = r;
	}
	
	public static void reset() {
		startRoom = null;
		bossRoom = null;
		rooms.clear();
		doors.clear();
		detector.end();
		tree = null;
	}
	
	public static ArrayList<Room> getRooms() {
		return rooms;
	}
	
	public static ArrayList<Door> getDoors() {
		return doors;
	}
	
	public static void addRoom(Room r) {
		rooms.add(r);
		tree.add(r);
	}
	
	public static void addDoor(Door d) {
		doors.add(d);
	}
	
	public static Path getPath(Room start, Room dest) {
		Path p = new Path();
		return tree.findPath(start, dest, p) ? p : null;
	}
	
    /**
     * In dungeon.
     *
     * @return true, if successful
     */
    public static boolean inDungeon() {
        return Widgets.get(945, 0).validate();
    }
	

}
